#include <stddef.h>
#include <stdint.h>

#include "../u8a.h"

#include "munit/munit.h"

static MunitResult
test_u8a(const MunitParameter params[], void *data)
{
        U8a_unit unit;
	U8a_len len;
	const char *str = "😊a¢";

        munit_assert_int(u8a_ahead_len("a"), ==, 1);
        munit_assert_int(u8a_ahead_len("¢"), ==, 2);
	munit_assert_int(u8a_ahead_len("€"), ==, 3);
	munit_assert_int(u8a_ahead_len("😊"), ==, 4);

	unit = u8a_get_unit("a", &len);
	munit_assert_int(u8a_unit_len(unit), ==, len);
	munit_assert_int(len, ==, 1);

	unit = u8a_get_unit("¢", &len);
	munit_assert_int(u8a_unit_len(unit), ==, len);
	munit_assert_int(len, ==, 2);

	unit = u8a_get_unit("€", &len);
	munit_assert_int(u8a_unit_len(unit), ==, len);
	munit_assert_int(len, ==, 3);

	unit = u8a_get_unit("😊", &len);
	munit_assert_int(u8a_unit_len(unit), ==, len);
	munit_assert_int(len, ==, 4);

	munit_assert_int(u8a_units("a¢€😊", 1 + 2 + 3 + 4), ==, 4);
	munit_assert_int(u8a_units_size_capped("😊a¢€", 2, 1 + 2 + 3 + 4),
			 ==, 4 + 1);

	str += u8a_ahead_len(str);
	str += u8a_ahead_len(str);
	str += u8a_ahead_len(str);


	munit_assert_int((len = u8a_back_len(str)), ==, 2);
	str -= len;
	munit_assert_int((len = u8a_back_len(str)), ==, 1);
	str -= len;
	munit_assert_int((len = u8a_back_len(str)), ==, 4);
	return MUNIT_OK;
}

static MunitTest test_suite_tests[] = {
	{ (char *) "/u8a", test_u8a,
	  NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
	{ NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL }
};

static const MunitSuite test_suite = {
	(char*) "u8a",
	test_suite_tests,
	NULL,
	1,
	MUNIT_SUITE_OPTION_NONE
};

int
main(int argc, char *argv[MUNIT_ARRAY_PARAM(argc + 1)])
{
	return munit_suite_main(&test_suite, NULL, argc, argv);
}
